package com.atguigu.atcrowdfunding.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.atcrowdfunding.bean.TRole;
import com.atguigu.atcrowdfunding.service.TRoleService;
import com.atguigu.atcrowdfunding.util.Datas;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class TRoleController {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	TRoleService roleService;
	
	@RequestMapping("/role/index")
	public String index() {
		return "role/index";
	}
	
	/**
	 * @ResponseBody注解
	 * 启用消息转换器:HttpMessageConverter
	 * 如果返回结果为对象类型(Entity class,List,Map..)
	 * 启用这个转换器->MappingJackson2HttpMessageConverter
	 * 将对象通过jackson序列化为json串
	 * 
	 * 如果返回结果为String类型
	 * 启用这个转换器->StringHttpMessageConverter
	 * 将字符串原样输出
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param model
	 * @return
	 */
	@RequestMapping("/role/loadData")
	@ResponseBody
	public PageInfo<TRole> loadData(@RequestParam(value="pageNum",required=false,defaultValue="1") Integer pageNum,
									@RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize,
									@RequestParam(value="condition",required=false,defaultValue="") String condition) {
		PageHelper.startPage(pageNum, pageSize);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("condition",condition);
		PageInfo<TRole> page = roleService.listRolePage(paramMap);
		log.debug("pageNum={}",pageNum);
		log.debug("pageSize={}",pageSize);
//		直接转换为json串返回
		return page;
	}
	
	@PreAuthorize("hasRole('PM - 项目经理')")
	@RequestMapping("/role/doAdd")
	@ResponseBody
	public String doAdd(TRole role) {
		try {
			roleService.saveTRole(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
//		直接转换为json串返回
		return "ok";
	}
	
	@RequestMapping("/role/getRoleById")
	@ResponseBody
	public TRole getRoleById(Integer roleId) {
		TRole role = roleService.getRoleById(roleId);
//		直接转换为json串返回
		return role;
	}
	
	@RequestMapping("/role/doUpdate")
	@ResponseBody
	public String doUpdate(TRole role) {
		try {
			roleService.updateTRole(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
//		直接转换为json串返回
		return "ok";
	}
	
	@RequestMapping("/role/doDelete")
	@ResponseBody
	public String doDelete(Integer roleId) {
		try {
			roleService.deleteRoleById(roleId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
//		直接转换为json串返回
		return "ok";
	}
	
	@RequestMapping("/role/doAssignPermissionToRole")
	@ResponseBody
	public String doAssignPermissionToRole(Integer roleId,Datas ds) {
		log.debug("roleId={}",roleId);
		log.debug("permissionIds={}",ds.getIds());
		try {
			roleService.saveRoleAndPermissionRelationship(roleId, ds.getIds());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ok";
	}
	
	@RequestMapping("/role/listPermissionIdByRoleId")
	@ResponseBody
	public List<Integer> listPermissionIdByRoleId(Integer roleId) {
		log.debug("roleId={}",roleId);
		List<Integer> list = null;
		try {
			list = roleService.listPermissionIdByRoleId(roleId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}
}
